package com.uraurora.crescent.function;

import com.google.common.cache.Cache;
import com.uraurora.crescent.function.functools.memoizer.MemoizerFunc1;
import com.uraurora.crescent.tuple.ITuple;

import java.util.Objects;
import java.util.concurrent.Future;


public interface Func1<T, RT> {

    RT on(T t);

    default <R> Func1<R, RT> before(Func1<? super R, ? extends T> before){
        Objects.requireNonNull(before);
        return r -> on(before.on(r));
    }

    default <V> Func1<T, V> after(Func1<? super RT, ? extends V> after) {
        Objects.requireNonNull(after);
        return t -> after.on(on(t));
    }

    //<editor-fold desc="记忆化">
    default MemoizerFunc1<T, RT> memoize(){
        return new MemoizerFunc1<>(this);
    }

    default MemoizerFunc1<T, RT> memoize(Cache<ITuple, Future<RT>> cache){
        return new MemoizerFunc1<>(this, cache);
    }
    //</editor-fold>
}
